// pages/dynamic/dynamicList/dynamicList.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr:[],
    pageNum:'1',//分页

    imgURL: app.globalData.imgURL,//图片总路径

  },
  
  //头像点击
  iconClick:function(e){
    wx.navigateTo({
      url: '../../home/shopMessage/shopMessage?id=' + e.currentTarget.id,
    })
  },

  pushDynamic:function(){
    var that = this;
    if (!wx.getStorageSync('userInfo').userId) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }

    if (wx.getStorageSync('userInfo').isShopMsg != 0) {
      wx.showToast({
        title: '请先创建店铺',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '../pushDynamic/pushDynamic',
    })
  },

  phoneClick: function (e) {
    console.log(e.currentTarget.id);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },

  //图片点击
  imgClick:function(e){
    var that = this;
    console.log(e);
    console.log(e.currentTarget.id);
    console.log(e.currentTarget.dataset.hang);
    var imgArr = that.data.listArr[e.currentTarget.id].img_url;
    var nImgArr = new Array();
    for(var i=0;i<imgArr.length;i++){
      nImgArr[i] = that.data.imgURL +imgArr[i].replace('_thum','');
    }
    console.log(nImgArr);

    wx.previewImage({
      current: nImgArr[e.currentTarget.dataset.hang],
      urls: nImgArr,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestData();
  },

  //请求数据
  requestData:function(){
    var that = this;
    app.post('act/list', { pageNum: that.data.pageNum }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var result = res.resultData;
        for (var i = 0; i < result.length; i++) {
          result[i].img_url = result[i].img_url.split(',');
        }
        // console.log(result);
        // that.setData({
        //   listArr: that.data.listArr.concat(result),
        // })
        if (that.data.pageNum == '1') {
          that.setData({
            listArr: result,
          })
        } else {
          that.setData({
            listArr: that.data.listArr.concat(result),
          })
        }
        console.log('长度');
        console.log(that.data.listArr[1].img_url.length);

        if (that.data.listArr.length == 0) {
          wx.showToast({
            title: '暂无数据',
            icon: 'none',
          })
        }
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageNum: '1',//分页
    })
    wx.showNavigationBarLoading();
    setTimeout(function () {
      that.requestData();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})